package com.amazon.dao;

import com.amazon.bo.Ean;
import com.amazon.bo.ShopGoodsBatch;
import com.amazon.dao.base.BaseDao;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class EanDao extends BaseDao<Ean> {


    public Ean selectLastEan() {
        List<Ean> eanList= execute(session -> session
                .createNativeQuery("SELECT * FROM ean where ean_status is null order by id desc  limit 1", Ean.class)
                .getResultList());
        return  eanList.size()>0?eanList.get(0):null;
    }

    public Map<String,Object> getIdMap(String id){

        String sql="\n" +
                "select de_id de,es_id es,fr_id fr,it_id it from browse_tree where en_id=:id";

        List<Map<String,Object>> list=(List<Map<String,Object>>) execute(session -> {
            Query query = session.createNativeQuery(sql);

            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            query.setParameter("id",id);

            return query.getResultList();
        });
        return list.size()>0?list.get(0):null;
    }
}
